package model.User.interceptor;

import io.jsonwebtoken.Claims;
import model.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



/**
 * 进入handler之前执行.如果返回false,则不放行,返回true,放行
 * Created by Sunshine、宸 on 2019/7/1.   弄一个全局异常处理器
 */
@Component
public class LoginInterceptor implements HandlerInterceptor{

    @Autowired
    private JwtUtil jwtUtil;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("经过了interceptor拦截器");
//        System.out.println(request.getContextPath()+"/user");/boot/user
        //从请求头信息获取token
        String token=request.getHeader("Authorization");
//        System.out.println(token);
       if(null!=token){
           //去掉前七位
           token=token.substring(7);
           //解析token
           //需要配置全局异常解析器
           Claims claims=null;
           if(null!=token&&token.length()>0) {
//               System.out.println(token instanceof String);
               claims = jwtUtil.parseJWT(token);
               if (!"null".equals(claims)) {

                   request.setAttribute("claims", claims);

               }

           }
       }
        return true;

    }
}
